<?php

class CpnewsController extends Core_Controller_Action 
{
	private $_news;
	
	public function init() {
	
		$this->_news = new News();
	}
	
	public function indexAction() {
		$this->_forward( 'list', 'cpnews' );
	}
	
	public function itemAction() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$type = intval( $this->_request->getParam( 'type', 0 ) );
		
		if( $type > 2 ) {
			$Categories = new Categries();
			$this->view->categoriesList = $Categories->getList( $type );
			$this->view->categories = array(0);
		}
		
		$this->view->type = $type;

		$Galeries = new Galeries();
		$this->view->galleriesList = $Galeries->getList();

		if( $this->_request->isPost() ) {
		
			$post = $this->_request->getPost();

			if( empty( $post['title_pl'] ) ) {
				$this->view->err_title_pl = 'Podaj tytuł';
			} else {

				if( $id ) {
					try {
						$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
						
						$conn->beginTransaction();
						$item = $this->_news->getItem( $id )->getFirst();
						
						foreach( $post as $key => $value ) {
		
							if( $key != 'categories' ) {
								$item->{$key} = $value;
							}
						}
						
						$item->save();
						
						if( $type > 2 ) {
							$CategriesHasNews = new CategriesHasNews();
							$CategriesHasNews->deleteByProduct( $id );
	
							if( isSet( $post['categories'] ) && is_array( $post['categories'] ) ) {
								foreach( $post['categories'] as $value ) {
									$CategriesHasNews = new CategriesHasNews();
									$CategriesHasNews->categries_c_id = $value;
									$CategriesHasNews->news_id = $id;
									$CategriesHasNews->save();
								}
							}
						}
						$this->_redirect( 'cpnews/list/type/' . $type . '/oper/2' );
						
					} catch( Exception $e ) {
						Core_Logs::log( 'carnauba', 'cpnews.exception', 'msg: ' . $e->getMessage() );
						Core_Logs::log( 'carnauba', 'cpnews.exception', 'code: ' . $e->getCode() );
						Core_Logs::log( 'carnauba', 'cpnews.exception', 'file: ' . $e->getFile() );
						Core_Logs::log( 'carnauba', 'cpnews.exception', 'line: ' . $e->getLine() );
						Core_Logs::log( 'carnauba', 'cpnews.exception', 'trace: ' . $e->getTraceAsString() );
						$conn->rollback();
						$this->_redirect( 'cpnews/list/type/' . $type . '/oper/3' );
					}
				} else {
					foreach( $post as $key => $value ) {
	
						if( $key != 'categories' ) {
							$this->_news->{$key} = $value;
						}
					}
					$this->_news->add_date = Date( 'Y-m-d H:i:s' );
					$this->_news->save();
					
					if( $this->_news->news_id > 0 && $type > 2 ) {
						if( isSet( $post['categories'] ) && is_array( $post['categories'] ) ) {
							foreach( $post['categories'] as $value ) {
								$CategriesHasNews = new CategriesHasNews();
								$CategriesHasNews->categries_c_id = $value;
								$CategriesHasNews->news_id = $this->_news->news_id;
								$CategriesHasNews->save();
							}
						}
					}

					$this->_redirect( 'cpnews/list/type/' . $type . '/oper/1' );
				}
			}

		} else {
			if( $id ) {
				$item = $this->_news->getItem( $id )->toArray();

				$CategriesHasNews = new CategriesHasNews();
				foreach( $CategriesHasNews->getByProduct( $id )->toArray() as $value ) {
					$this->view->categories[] = $value['categries_c_id'];
				}

				foreach( $item[0] as $key => $value ) {
					$this->view->{$key} = $value;
				}
			}
		
		}
		
		$this->assignType( $type );
	}
	
	
	public function item2Action() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$type = intval( $this->_request->getParam( 'type', 0 ) );
		
		$Categories = new Categries();
		$this->view->categoriesList = $Categories->getList( $type );
		$this->view->categories = array(0);

		$Galeries = new Galeries();
		$this->view->galleriesList = $Galeries->getList();

		if( $this->_request->isPost() ) {
		
			$post = $this->_request->getPost();

			if( empty( $post['title'] ) ) {
				$this->view->err_title_pl = 'Podaj tytuł';
			} else {

				if( $id ) {
					try {
						$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
						
						$conn->beginTransaction();
						$item = $this->_news->getItem( $id )->getFirst();
						foreach( $post as $key => $value ) {
		
							if( $key != 'categories' ) {
								$item->{$key} = $value;
							}
						}
						$item->save();

						$CategriesHasNews = new CategriesHasNews();
						$CategriesHasNews->deleteByProduct( $id );

						if( isSet( $post['categories'] ) && is_array( $post['categories'] ) ) {
							foreach( $post['categories'] as $value ) {
								$CategriesHasNews = new CategriesHasNews();
								$CategriesHasNews->categries_c_id = $value;
								$CategriesHasNews->news_id = $id;
								$CategriesHasNews->save();
							}
						}

						$this->_redirect( 'cpnews/list/type/' . $type . '/oper/2' );
						
					} catch( Exception $e ) {
						Core_Logs::log( 'carnauba', 'cpnews.exception', 'msg: ' . $e->getMessage() );
						Core_Logs::log( 'carnauba', 'cpnews.exception', 'code: ' . $e->getCode() );
						Core_Logs::log( 'carnauba', 'cpnews.exception', 'file: ' . $e->getFile() );
						Core_Logs::log( 'carnauba', 'cpnews.exception', 'line: ' . $e->getLine() );
						Core_Logs::log( 'carnauba', 'cpnews.exception', 'trace: ' . $e->getTraceAsString() );
						$conn->rollback();
						$this->_redirect( 'cpnews/list/type/' . $type . '/oper/3' );
					}
				} else {
					foreach( $post as $key => $value ) {
	
						if( $key != 'categories' ) {
							$this->_news->{$key} = $value;
						}
					}
					$this->_news->add_date = Date( 'Y-m-d H:i:s' );
					$this->_news->save();
					
					if( $this->_news->news_id > 0 ) {
						if( isSet( $post['categories'] ) && is_array( $post['categories'] ) ) {
							foreach( $post['categories'] as $value ) {
								$CategriesHasNews = new CategriesHasNews();
								$CategriesHasNews->categries_c_id = $value;
								$CategriesHasNews->news_id = $this->_news->news_id;
								$CategriesHasNews->save();
							}
						}
					}

					$this->_redirect( 'cpnews/list/type/' . $type . '/oper/1' );
				}
			}

		} else {
			if( $id ) {
				$item = $this->_news->getItem( $id )->toArray();

				$CategriesHasNews = new CategriesHasNews();
				foreach( $CategriesHasNews->getByProduct( $id )->toArray() as $value ) {
					$this->view->categories[] = $value['categries_c_id'];
				}

				foreach( $item[0] as $key => $value ) {
					$this->view->{$key} = $value;
				}
			}
		
		}
		
		$this->assignType( $type );
	}
	
	
	
	
	public function listAction() {
		$type = intval( $this->_request->getParam( 'type', 0 ) );
		$page = intval( $this->_request->getParam( 'page', 1 ) );

		$pagerLayout = $this->_news->getListWithPager( $type, $page, $this->view->baseUrl() );

		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;

		$this->assignType( $type );
	}
	
	public function specialAction() {
		
		$this->view->menu = array(
			array('name' => 'Akademia autodetailingu', 'id'=>'1'),
			array('name' => 'Sklep', 'id'=>'2')
			);
			
		$this->view->yih2 = 'Teksty specjalne';
	}
	
	public function editspecialAction()
	{
		
		$opper = intval( $this->_request->getParam( 'opper', 0 ) );
		$id = intval( $this->_request->getParam( 'id', 0) );
		
		
		$Galeries = new Galeries();
		$this->view->galleriesList = $Galeries->getList();
		$this->view->yih2 = 'Teksty specjalne';
		
		$tmp = "";
		
		if($id == 1)
			$tmp = "aad";
		if($id == 2)
			$tmp = "shp";
			
		$this->view->id = $id;
		
		$specItem = new News();
		$specItem = $specItem->getSpecialItem($tmp)->getFirst();
		
		if($specItem)
		{
					$specItem = $specItem->toArray();
					
					foreach( $specItem as $key => $value ) 
					{
						if($key != 'main_page')
							$this->view->{$key} = $value;
						else
							$this->view->main_page =  $specItem['news_id'];
					}
		}
		
		if( $this->_request->isPost() ) 
		{
			$post = $this->_request->getPost();

			
			if( empty( $post['title_pl'] ) ) 
			{
				$this->view->err_title_pl = 'Podaj tytuł';
			} 
			else 
			{
				if(empty($post['main_page']))
				{
					$special_news = new News();
					
					foreach( $post as $key => $value ) 
					{
						if($key != 'main_page')
							$special_news->{$key} = $value;
						else
							$special_news->main_page = $tmp;
					}
					$special_news->save();
					
					$this->_redirect( 'cpnews/special');
				}
				else
				{
					$special_news = new News();
					$special_news = $special_news->getItem($post['main_page'])->getFirst();
					
					foreach( $post as $key => $value ) 
					{
						if($key != 'main_page')
							$special_news->{$key} = $value;
						else
							$special_news->main_page = $tmp;
						
					}
					$special_news->save();
					$this->_redirect( 'cpnews/special');
				}
			}
		}
	}
	
	public function deleteAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$type = intval( $this->_request->getParam( 'type', 0 ) );
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		if( $id > 0 ) {
			$this->_news->deleteItem( $id );
			
			$this->_redirect( 'cpnews/list/type/' . $type . '/oper/4' );
		} else {
			$this->_redirect( 'cpnews/list/type/' . $type . '/oper/3' );
		}
	}
	
	public function assignType( $type ) {
		
		$this->view->type = $type;
		
		switch( $type ) {
			case 1 :
				$this->view->yih2 = 'Relacje';
				break;
			case 2 :
				$this->view->yih2 = 'Aktualności';
				break;
			case 3 :
				$this->view->yih2 = 'Aktualności autodetailing';
				break;
			case 5 :
				$this->view->yih2 = 'Współpraca';
				break;
			case 6 :
				$this->view->yih2 = 'Usługi';
				break;
		}
	}
}